package j.todo;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SBSurfaceView extends SurfaceView implements
		SurfaceHolder.Callback {
	private Context mContext;
	private SBSurfaceViewThread sbSurfaceViewThread;
	private boolean hasSurface;
	private float screenWidth, screenHeight;
	private int x, y;

	// private Bitmap photo;

	public SBSurfaceView(Context context) {
		super(context);
		mContext = context;
		hasSurface = false;
		resume();
	}

	public void resume() {
		if (null == sbSurfaceViewThread) {
			sbSurfaceViewThread = new SBSurfaceViewThread();
			sbSurfaceViewThread.setSurfaceHolder(getHolder(), this);
			if (hasSurface) {
				sbSurfaceViewThread.start();
			}
		}
	}

	public void pause() {
		if (null != sbSurfaceViewThread) {
			sbSurfaceViewThread.requestExitAndWait();
			sbSurfaceViewThread = null;
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (null != sbSurfaceViewThread) {
			sbSurfaceViewThread.onWindowResize(width, height);
		}
	}

	public void surfaceCreated(SurfaceHolder holder) {
		hasSurface = true;

		if (null != sbSurfaceViewThread) {
			sbSurfaceViewThread.start();
		}
		resume();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		hasSurface = false;
		pause();
	}

	// public void addItem(SBItemButton item) {
	// if (null != item) {
	// SBDefineConstant.getInstance().getItemList().add(item);
	// }
	// if (null == sbSurfaceViewThread) {
	// sbSurfaceViewThread = new SBSurfaceViewThread();
	// sbSurfaceViewThread.setSurfaceHolder(getHolder(), this);
	// if (hasSurface) {
	// sbSurfaceViewThread.start();
	// }
	// }
	// sbSurfaceViewThread
	// .setReqCode(SBDefineConstant.getInstance().REQ_DRAW_ITEM);
	// resume();
	// }

	public void drawDefault() {
		if (null == sbSurfaceViewThread) {
			sbSurfaceViewThread = new SBSurfaceViewThread();
			sbSurfaceViewThread.setSurfaceHolder(getHolder(), this);
			if (hasSurface) {
				sbSurfaceViewThread.start();
			}
		}
		// sbSurfaceViewThread
		// .setReqCode(SBDefineConstant.getInstance().REQ_DRAW_ITEM);
		resume();
	}

	public void drawItemList() {
		if (null == sbSurfaceViewThread) {
			sbSurfaceViewThread = new SBSurfaceViewThread();
			sbSurfaceViewThread.setSurfaceHolder(getHolder(), this);
			if (hasSurface) {
				sbSurfaceViewThread.start();
			}
		}
		// sbSurfaceViewThread
		// .setReqCode(SBDefineConstant.getInstance().REQ_DRAW_ITEM);
		resume();

	}

	public SBSurfaceViewThread getSBSurfaceviewThread() {
		return sbSurfaceViewThread;
	}

	public void drawIntro(Bitmap intro) {
		if (null != intro) {
			sbSurfaceViewThread.setIntro(intro);
			// sbSurfaceViewThread
			// .setReqCode(SBDefineConstant.getInstance().REQ_DRAW_INTRO);
		}
	}

	public void setRequestCode(int reqCode) {
		sbSurfaceViewThread.setReqCode(reqCode);
	}

	// public void setRunningMode(boolean mode) {
	// if (mode) {
	// hasSurface = true;
	// sbSurfaceViewThread.setDone(true);
	//
	// if (null != sbSurfaceViewThread) {
	// sbSurfaceViewThread.start();
	// }
	// resume();
	// } else {
	// hasSurface = false;
	// sbSurfaceViewThread.setDone(false);
	// pause();
	// }
	// }
}
